//
// Created by EDZ on 2022/1/5.
//

#include "exception.h"
#include <iostream>

void exception_int() {
    int x = -1;
    try {
        if (x<0){
            throw x;
        }
    }catch (int x) {
        std::cout << "异常发生了：异常类型为内置类型 int型：" << x << std::endl;
    }
}

void func_exception() {
    int age;
    std::cout << "请输入年龄（小于10岁会抛异常）: ";
    std::cin  >> age;
    try {
        if (age < 10) {
            throw bad_age_error("自定义错误 bad_age_error! 年龄错了");
        }
        std::cout << "年龄正常 "<< age <<" 没有异常！\n" ;
    } catch (bad_age_error& er) {
        std::cout << "异常发生了： 抛出的异常值为：" << er.what() << std::endl;
        exception_int();
    }
}

char const *bad_age_error::what() const noexcept {
    return error.c_str();
}
